var express = require('express');
var router = express.Router();
var userService = require('../service/usersService.js')
/* GET users listing. */
// 查询
router.post('/serch', async function (req, res, next) {
  let { type, value } = req.body;
  let searchObj = {};
  //模糊查询
  if (type && value) {
    if (type == 'name') {
      searchObj[type] = { $regex: value };
    } else {
      searchObj[type] = { $regex: `^${value}` };
    }
  }
  console.log(searchObj);
  let data = await userService.findSeach(searchObj);
  res.send(data);

})
// 查询所有
router.get('/All', async function (req, res, next) {
  let data = await userService.findAll();
  res.send(data);

})


// 增加
router.post('/add', async function (req, res, next) {
  let data = await userService.insert(req.body)
  res.send(data);
});

// 删除
router.delete('/delelt/:id', async function (req, res, next) {
  let id = req.params.id
  let data = await userService.insert(id)
  res.send(data);
});


// 修改
router.put('/updata/:id', async function (req, res, next) {
  let id = req.params.id
  let data = await userService.updata(id, req.body)
  res.send(data);
});


// 登录
router.get('/login/:id', async function (req, res, next) {
  let id=req.params.id
  let data = await userService.login(id);
  res.send(data);

})





module.exports = router;
